package util

import java.text.SimpleDateFormat
import java.util.Calendar
import org.apache.commons.lang.time.DateUtils
import org.json4s.JsonDSL._
import org.json4s._
import org.json4s.jackson.JsonMethods._
import  org.json4s.JsonAST._
import com.typesafe.config._

import scala.util.Try

class ConfigContext(config: Config) {
  config.checkValid(ConfigFactory.defaultReference(), "util")
  def this() {
    //load  qa env
    // this(ConfigFactory.load("qaconfig.conf"))
    //load product env
       this(ConfigFactory.load("application.conf"))
  }
  def get(path: String)=config.getString(path)
}
object Util {
  val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
  def getConfig(path:String)=new ConfigContext().get(path)

  //把用户信息转换json
  def  toUserJobejct(userInfo:(String,Long),report_date:String="")={
    val(pk,count)=(userInfo._1.split("%"),userInfo._2)
    pk.length match {
      case 1 => {
        if(report_date.isEmpty)
          ("product_key"->pk(0)) ~ ("count" -> count)
        else
          ("product_key"->pk(0)) ~ ("count" -> count)~("report_date"->report_date)
      }
      case 2 =>{
        ("product_key"->pk(0)) ~ ("group" ->Map(pk(1) ->count))
      }
      }
    }
 // 把设备相关信息转换json
  def  toJobejct(deviceInfo:(String,Long),report_date:String="")={
   val(pk,count)=(deviceInfo._1.split("%"),deviceInfo._2)
    pk.length match {
      case 1 => {
        if(report_date.isEmpty)
        ("product_key"->pk(0)) ~ ("count" -> count)
        else
          ("product_key"->pk(0)) ~ ("count" -> count)~("report_date"->report_date)
      }
      case 2 =>{
        ("product_key"->pk(0)) ~ ("group" ->Map(pk(1) ->Map("count"->count)))
      }
      case 3 =>{
        if (pk(1).equalsIgnoreCase("location"))
          ("product_key"->pk(0)) ~ ("location" ->Map(pk(2) ->Map("count"->count) ))
        else
          ("product_key"->pk(0)) ~ ("group" ->Map(pk(1) ->Map("location"->Map(pk(2)->Map("count"->count)))))
      }
      case 4 =>{
        if (pk(1).equalsIgnoreCase("location"))
          ("product_key"->pk(0)) ~ ("location" ->Map(pk(2) ->Map(pk(3)->Map("count"->count) )))
        else
          ("product_key"->pk(0)) ~ ("group" ->Map(pk(1) ->Map("location"->Map(pk(2)->Map( pk(3)->Map("count"->count) )))))
      }
      case 5 =>{
        if (pk(1).equalsIgnoreCase("location"))
          ("product_key"->pk(0)) ~ ("location" ->Map(pk(2) ->Map(pk(3)->Map( pk(4)->Map("count"->count)) )))
        else
          ("product_key"->pk(0)) ~ ("group" ->Map(pk(1) ->Map("location"->Map(pk(2)->Map( pk(3)->Map(pk(4)->Map("count"->count)) )))))
      }

    }
  }
  /**
   *
   * @param day  DayofWeek("2015-05-28")
   * @return   2015-05-25
   */

   def DayofWeek(day:String):String= Try{
    val  instance = Calendar.getInstance()
    instance.setTime(dateFormat.parse(day))
    instance.setFirstDayOfWeek(Calendar.MONDAY)
    if(1 == instance.get(Calendar.DAY_OF_WEEK))instance.add(Calendar.DAY_OF_MONTH, -1)
    instance.add(Calendar.DATE, instance.getFirstDayOfWeek()-instance.get(Calendar.DAY_OF_WEEK))
    dateFormat.format(instance.getTime)
  }.getOrElse("")

  /**
   *
   * @param day   //  MonthofYear("2015-05-25")
   * @return 2015-05-01
   */

  def MonthofYear(day:String):String=Try{
    val  date = dateFormat.parse(day)
    val  instance = Calendar.getInstance()
    instance.setTime(date)
    val  i = instance.get(Calendar.DAY_OF_MONTH)
    dateFormat.format(DateUtils.addDays(date, -i + 1))
  }.getOrElse("")

  /**
   *
   * @param day   getWeek("2015-05-25")
   * @return  2015-22
   */

  def getWeek(day:String):String=Try{
    val  instance = Calendar.getInstance()
    instance.setTime(dateFormat.parse(day.toString()))
    instance.setFirstDayOfWeek(Calendar.MONDAY)
    instance.setMinimalDaysInFirstWeek(4)
    val  Calendarday = instance.get((Calendar.WEEK_OF_YEAR))
    val year = instance.getWeekYear
    if (Calendarday<10) year.toString+"-0"+Calendarday.toString
    else  year.toString+"-"+Calendarday.toString
  }.getOrElse("")


  /** *
    *
    * @param day  getMonth("2015-01-25")
    * @return  2015-01
    */
  def getMonth(day:String):String= Try{
    val  instance = Calendar.getInstance()
    instance.setTime(dateFormat.parse(day.toString()))
    val  CalendarMonth = instance.get(Calendar.MONTH)+1
    val year = instance.get(Calendar.YEAR)
    if(CalendarMonth<10) year.toString+"-0"+CalendarMonth.toString
    else  year.toString+"-"+CalendarMonth.toString
  }.getOrElse("")

  /**
   *
   * @param day   //addDay("2015-01-25",-1)
   * @param incr
   * @return  2015-01-24
   */

  def addDay(day:String,incr:Int):String=Try{
    val date = dateFormat.parse(day.toString())
    val instance = Calendar.getInstance()
    instance.setTime(date)
    dateFormat.format(DateUtils.addDays(date, incr))
  }.getOrElse("")

  /**
   *
   * @param date  conv2ts("2015-05-09")
   * @return  Unknown
   */
  def  conv2ts(date:String):Long=Try(dateFormat.parse(date.toString).getTime/1000L.toLong).getOrElse(0L)

  /**
   *
   * @param loc  replaceNull("null")
   * @return  Unknown
   */
  def  replaceNull(loc:String)=loc  match{
    case "null"=>"Unknown"
    case ""=>"Unknown"
    case null =>"Unknown"
    case loc =>loc

  }

}
